package com.sunny.ihealth.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RadioGroup;

import com.sunny.ihealth.R;
import com.sunny.wheelnumberpicker.WheelNumberPicker;

import java.util.HashMap;
import java.util.Map;

/**
 * 选择数字的dialog
 * Created by Sunny  An on 2016/5/20.
 */
public class WheelDialog {

    public interface OnPressOKListener {
        void done(WheelType wheelType, RunType runType, float number);
    }

    public enum WheelType {
        WALK, RUN, SLEEP
    }

    public enum RunType {
        DISTANCE, TIME, NONE
    }

    public static Dialog getWheelDialog(Context context, final WheelType wheelType, RunType runType, float[] defaultValue, final OnPressOKListener listener) {
        final Map<String, Float> temp = new HashMap<>();

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_wheel, null);
        final WheelNumberPicker wheelNumberPicker = (WheelNumberPicker) view.findViewById(R.id.wheel);
        final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup_dialog_util);
        switch (wheelType) {
            case WALK:
                wheelNumberPicker.setNumber(defaultValue[0]);
                radioGroup.setVisibility(View.GONE);
                wheelNumberPicker.setUnitText("步");
                wheelNumberPicker.setMinNumber(100);
                wheelNumberPicker.setNumOfBlocks(20);
                wheelNumberPicker.setBlockSize(100);
                break;
            case RUN:
                radioGroup.setVisibility(View.VISIBLE);
                if (runType == RunType.DISTANCE) {
                    temp.put("temp", defaultValue[1]);
                    wheelNumberPicker.setNumber(defaultValue[0]);
                    radioGroup.check(R.id.radioBtn_dialog_util_km);
                    wheelNumberPicker.setUnitText("公里");
                    wheelNumberPicker.setMinNumber(0.1F);
                    wheelNumberPicker.setNumOfBlocks(20);
                    wheelNumberPicker.setBlockSize(0.1F);
                } else {
                    temp.put("temp", defaultValue[0]);
                    wheelNumberPicker.setNumber(defaultValue[1]);
                    radioGroup.check(R.id.radioBtn_dialog_util_minute);
                    wheelNumberPicker.setUnitText("分钟");
                    wheelNumberPicker.setMinNumber(1);
                    wheelNumberPicker.setNumOfBlocks(30);
                    wheelNumberPicker.setBlockSize(1);
                }
                break;
            case SLEEP:
                wheelNumberPicker.setNumber(defaultValue[0]);
                radioGroup.setVisibility(View.GONE);
                wheelNumberPicker.setUnitText("分钟");
                wheelNumberPicker.setMinNumber(2 * 60F);
                wheelNumberPicker.setMaxNumber(12 * 60F);
                wheelNumberPicker.setNumOfBlocks(30);
                wheelNumberPicker.setBlockSize(1F);
                break;
        }

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                float num = wheelNumberPicker.getNumber();
                wheelNumberPicker.setNumber(temp.get("temp"));
                if (checkedId == R.id.radioBtn_dialog_util_km) {  //公里
                    wheelNumberPicker.setUnitText("公里");
                    wheelNumberPicker.setMinNumber(0.1F);
                    wheelNumberPicker.setNumOfBlocks(20);
                    wheelNumberPicker.setBlockSize(0.1F);
                } else {  //分钟
                    wheelNumberPicker.setUnitText("分钟");
                    wheelNumberPicker.setMinNumber(1);
                    wheelNumberPicker.setNumOfBlocks(30);
                    wheelNumberPicker.setBlockSize(1);
                }
                temp.put("temp", num);
            }
        });

        return new AlertDialog.Builder(context)
                .setTitle("转动圆盘来设置您的目标")
                .setView(view)
                .setNegativeButton("取消", null)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (listener != null) {
                            RunType type;
                            if (wheelType != WheelType.RUN) {
                                type = RunType.NONE;
                            } else {
                                type = radioGroup.getCheckedRadioButtonId() == R.id.radioBtn_dialog_util_km ? RunType.DISTANCE : RunType.TIME;
                            }
                            listener.done(wheelType, type, wheelNumberPicker.getNumber());
                        }
                    }
                })
                .create();
    }
}
